#!/bin/bash
##############################################################
source ./pkgutils.sh || exit 1
PKG_NAME=$(pkgname); if (( $? != 0)); then exit 2; fi
PKG_VERSION=$(pkgversion); if (( $? != 0)); then exit 3; fi
if [[ $(whoami) != "root" ]]; then
	echo "Please log in as user root...";
	exit 4;
fi
mkdir -pv ./tmp && cd ./tmp || exit 5;
##############################################################
# do setup tasks (location: /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp)

echo "setup of $PKG_NAME $PKG_VERSION";
echo 'main() {}' > dummy.c && \
cc dummy.c -v -Wl,--verbose &> dummy.log && \
RELF="$(readelf -l a.out)";
if [[ $(echo "$RELF" | grep 'interpreter: /lib') == "" ]]; then
	errcho "Wrong readelf result:";
	errcho "$RELF";
	exit 13;
fi

# check in detail
errcho 'Check compiling log in detail... searching for "/usr/lib.*/crt[1in].*succeeded"';
CRTLNO=$(grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log | wc -l);
if (($CRTLNO < 3)); then
	errcho "$(grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log)";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 14;
fi;
errcho 'Check compiling log in detail... searching for "^ /usr/include"';
INCPATH="$(grep -B4 '^ /usr/include' dummy.log)";
if [[ $(echo "$INCPATH" | grep '^#include .* search starts here:') == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 15;
fi;
if [[ $(echo "$INCPATH" | grep "/usr/lib/gcc/.*/$PKG_VERSION/include") == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 18;
fi;
if [[ $(echo "$INCPATH" | grep "/usr/local/include") == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 19;
fi;
if [[ $(echo "$INCPATH" | grep "/usr/lib.*/gcc/.*/$PKG_VERSION/include-fixed") == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 20;
fi;
if [[ $(echo "$INCPATH" | grep '/usr/include') == "" ]]; then
	errcho "$INCPATH";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 21;
fi;
errcho 'Check compiling log in detail... searching for "SEARCH.*/usr/lib"';
SEARCHDIR="$(grep 'SEARCH.*/usr/lib' dummy.log | sed 's|; |\n|g')";
if (( $(echo "$SEARCHDIR" | grep '("/usr/lib.*")' | wc -l) < 2 || $(echo "$SEARCHDIR" | grep '("/lib.*")' | wc -l) < 2  || $(echo "$SEARCHDIR" | grep '("/usr/.*/lib.*")' | wc -l) < 4 )); then
	errcho "$SEARCHDIR";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 16;
fi;
errcho 'Check compiling log in detail... searching for "/lib.*/libc.so.6 succeeded"';
LIBCSO6="$(grep "/lib.*/libc.so.6 succeeded" dummy.log)";
if [[ $LIBCSO6 == "" ]]; then
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 17;
fi;
errcho 'Check compiling log in detail... searching for "ld-linux.*.so.2 at /lib.*/ld-linux.*.so.2"';
FOUND="$(grep 'ld-linux.*.so.2 at /lib.*/ld-linux.*.so.2' dummy.log)";
if [[ $FOUND == "" ]]; then
	errcho "$FOUND";
	errcho "Compiling log file $(pwd)/dummy.log didn't match... ";
	exit 8;
fi
rm -vf dummy.c a.out dummy.log



##############################################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package setup with code $ERR";
    exit 6;
fi
